package com.example.controller;

import com.example.entity.Department;
import com.example.entity.Teacher;
import com.example.entity.example.DepartmentExample;
import com.example.entity.example.TeacherExample;
import com.example.entity.vo.TeacherVO;
import com.example.service.DepartmentService;
import com.example.service.TeacherService;
import com.example.util.CustomConstant;
import com.example.util.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/teacher")
@Controller
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    @Autowired
    DepartmentService departmentService;

    public TeacherVO transformToTeacherVO(Teacher teacher) {
        TeacherVO teacherVO = new TeacherVO();
        BeanUtils.copyProperties(teacher, teacherVO);

        Integer departmentId = teacher.getDepartmentId();
        Department department = departmentService.getById(departmentId);
        if (department != null) {
            teacherVO.setDepartmentName(department.getName());
        }
        return teacherVO;
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("/get.json")
    public ResultEntity<List<TeacherVO>> get(@RequestParam("page") Integer page,
                                             @RequestParam("limit") Integer limit,
                                             @RequestParam(value = "id", required = false) String id,
                                             @RequestParam(value = "name", required = false) String name,
                                             @RequestParam(value = "gender", required = false) String gender,
                                             @RequestParam(value = "title", required = false) String title
    ) {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        if (id != null && !id.isEmpty()) {
            id = '%' + id + '%';
            criteria.andIdLike(id);
        }
        if (name != null && !name.isEmpty()) {
            name = '%' + name + '%';
            criteria.andNameLike(name);
        }
        if (title != null && !title.isEmpty()) {
            criteria.andTitleEqualTo(title);
        }
        if (gender != null && !gender.isEmpty()) {
            criteria.andGenderEqualTo(gender);
        }
        List<Teacher> teachers = teacherService.getByExampleWithLimit(example, page, limit);
        List<TeacherVO> teacherVOs = new ArrayList<>(teachers.size());
        for (Teacher teacher : teachers) {
            TeacherVO teacherVO = this.transformToTeacherVO(teacher);
            teacherVOs.add(teacherVO);
        }
        long count = teacherService.getCountByExample(example);
        return ResultEntity.createResultEntity(0, null, count, teacherVOs);
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @RequestMapping("/add/page.html")
    public String addPage(ModelMap modelMap) {
        List<Department> deptList = departmentService.getByExampleWithLimit(new DepartmentExample(), 1, Integer.MAX_VALUE);
        modelMap.addAttribute("deptList", deptList);
        return "teacher/teacher-add";
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("add")
    public ResultEntity<Object> add(@RequestBody Teacher teacher) {
        String id = teacher.getId();
        if (teacherService.getById(id) != null) {
            return ResultEntity.failedWithoutData(CustomConstant.MESSAGE_THE_STUDENT_ID_ALREADY_EXISTS);
        }
        teacherService.add(teacher);
        return ResultEntity.successWithoutData();
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @RequestMapping("/{id}/modify/page.html")
    public String modifyPage(ModelMap modelMap, @PathVariable("id") String id) {
        Teacher teacher = teacherService.getById(id);
        List<Department> deptList = departmentService.getByExampleWithLimit(new DepartmentExample(), 1, Integer.MAX_VALUE);
        modelMap.addAttribute("teacher", teacher);
        modelMap.addAttribute("deptList", deptList);
        return "teacher/teacher-modify";
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("/update")
    public ResultEntity<Object> update(@RequestBody Teacher teacher) {
        teacherService.updateById(teacher);
        return ResultEntity.successWithoutData();
    }

    @PreAuthorize("hasRole('ROLE_管理员')")
    @ResponseBody
    @RequestMapping("/remove")
    public ResultEntity<Object> remove(@RequestParam("id") String id) {
        teacherService.removeById(id);
        return ResultEntity.successWithoutData();
    }
}
